<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login(){
    	$this->display();
	}
	public function do_login(){
		$rules = array(
		     array('verify','require','验证码必须！'),
		     array('username','require','用户名必须！'),
		     array('password','require','密码必须！'),
		);
		$user = M("user"); // 实例化User对象
		if (!$user->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($user->getError());
		}else{
			 $verify = new \Think\Verify();
    		 if(!$verify->check(I('post.verify'))){
    		 	$this->error('验证码错误');
    		 };
		     $username=I('post.username');
		     $password=md5(I('post.password'));
		     $where=array(
		     	'username'=>$username,
		     	'password'=>$password
		     	);
		     $user=M('user')->where($where)->find();
		     if(!$user){
		     	$this->error('用户名或密码错误');
		     }
		     $status=$user['status'];
		     if($status==0){
		     	$this->error('用户被禁用',U('home/front/index'));
		     }
		     $_SESSION['user_is_login']=true;
		     $_SESSION['username']=$username;
		     $_SESSION['user_id']=$user['id'];
		     $this->redirect('front/index');
		}
	}
	public function verify(){
			$Verify = new \Think\Verify();
			$Verify->length=3;
			$Verify->entry();
	}
	public function regedit(){
		$this->display();
	}
	public function do_regedit(){
			$rules = array(
		     array('verify','require','验证码必须！'),
		     array('username','require','用户名必须！'),
		     array('password','require','密码必须！'),
		     array('passwordagain','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
		);
		$user = M("user"); // 实例化User对象
		if (!$user->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($user->getError());
		}else{
			$verify = new \Think\Verify();
			if(!$verify->check(I('post.verify'))){
				$this->error('验证码错误');
			};
			$username=I('post.username');
			$password=md5(I('post.password'));
			$user=M('user')->where('username='.$username)->find();
			//$user=M('user')->where(array('username'=>$username))->find();
			if($user){
				$this->error('用户名已经被注册');
			};
			$data['username'] = $username;
			$data['password'] = $password;
			$data['creat_time'] = time();
			$User = M('user');
			$User->data($data)->add();
			$this->success('注册成功，请登录','login');
		}
	}
	public function do_logout(){
		session_destroy();
		 $this->redirect('front/index');
	}
	
	 
}